package com.brsya.movie.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.util.TypedValue;
import android.view.View;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.brsya.base.base.BaseMvpFragment;
import com.brsya.base.base.Contents;
import com.brsya.base.bean.HomeTabBean;
import com.brsya.base.util.UiUtil;
import com.brsya.movie.R;
import com.brsya.movie.activity.FindActivity;
import com.brsya.movie.activity.PlayHistoryActivity;
import com.brsya.movie.activity.SearchActivity;
import com.brsya.movie.contract.HomeContract;
import com.brsya.movie.presenter.HomePresenter;
import com.flyco.tablayout.SlidingTabLayout;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Brsya
 * CreateDate: 2022/6/29  20:18
 * Description:
 * 首页
 */
public class HomeFragment extends BaseMvpFragment<HomePresenter> implements HomeContract.View, View.OnClickListener {
    private int mainIndex = 0;
    private LinearLayout llHomeTitleBox;
    private LinearLayout llHomeTitleSearchBox;
    private View vHomeTitleHistory;
    private View vHomeTitleFilter;
    private SlidingTabLayout homeTitleTab;
    private ViewPager vpHomePageBox;

    // 保存所有Fragment
    private List<Fragment> tabFragmentList;
    // 保存标题
    private List<String> tabNames;
    private Handler mainHandler;
    // 设置小说头部Padding 适配状态栏
    private Runnable bookChangePaddingRunnable;

    private MovieListFragment.OnClickTabListener onClickTabListener;

    public static HomeFragment newInstance() {

        Bundle args = new Bundle();

        HomeFragment fragment = new HomeFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected void initView() {
        setContentViewId(R.layout.fragment_home);

        llHomeTitleBox = findViewById(R.id.ll_home_title_box);
        llHomeTitleSearchBox = findViewById(R.id.ll_home_title_search_box);
        vHomeTitleHistory = findViewById(R.id.v_home_title_history);
        vHomeTitleFilter = findViewById(R.id.v_home_title_filter);
        homeTitleTab = findViewById(R.id.home_title_tab);
        vpHomePageBox = findViewById(R.id.vp_home_page_box);


        initViewData();
    }

    private void initViewData() {

        UiUtil.setStatusPadding(getContext(), llHomeTitleBox);

        tabFragmentList = new ArrayList<>();
        tabNames = new ArrayList<>();
        mainHandler = new Handler(Looper.getMainLooper());

//        tabNames.add("小说");
//        tabFragmentList.add(BookFragment.newInstance());
        tabNames.add("推荐");
        tabFragmentList.add(NewsFragment.newInstance());
        mainIndex = tabNames.size();

        llHomeTitleSearchBox.setOnClickListener(this);
        vHomeTitleHistory.setOnClickListener(this);
        vHomeTitleFilter.setOnClickListener(this);


    }

    @Override
    protected void initData() {
        presenter.getHomeTab();
    }

    @Override
    public void showTabData(HomeTabBean homeTabBeans) {
        for (int i = 0; i < homeTabBeans.getTypeList().size(); i++) {
            HomeTabBean.TypeListDTO typeListDTO = homeTabBeans.getTypeList().get(i);
            tabNames.add(typeListDTO.getCategoryName());
            if (i == 0) {
                // 添加精选布局
                MovieListFragment movieListFragment = MovieListFragment.newInstance(null);
                movieListFragment.setOnClickTabListener(onClickTabListener);
                tabFragmentList.add(movieListFragment);
            } else {
                // 添加其他布局
                tabFragmentList.add(MovieListFragment.newInstance(typeListDTO.getCode()));
            }

        }
        vpHomePageBox.setOffscreenPageLimit(tabNames.size());
        vpHomePageBox.setAdapter(new FragmentPagerAdapter(getParentFragmentManager(), FragmentPagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT) {
            @NonNull
            @Override
            public Fragment getItem(int position) {
                return tabFragmentList.get(position);
            }

            @Override
            public int getCount() {
                return tabNames.size();
            }

            @Nullable
            @Override
            public CharSequence getPageTitle(int position) {
                return tabNames.get(position);
            }
        });
        vpHomePageBox.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                for (int i = 0; i < tabNames.size(); i++) {
                    if (position == i) {
                        homeTitleTab.getTitleView(i).setTextSize(TypedValue.COMPLEX_UNIT_SP, 20);
                    } else {
                        homeTitleTab.getTitleView(i).setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);
                    }
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        homeTitleTab.setViewPager(vpHomePageBox);
        homeTitleTab.getTitleView(0).setTextSize(TypedValue.COMPLEX_UNIT_SP, 20);
        vpHomePageBox.setCurrentItem(mainIndex);
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        int position = vpHomePageBox.getCurrentItem();
        if (0 <= position && position < tabFragmentList.size()) {
            if (hidden) {
                tabFragmentList.get(position).onPause();
            }else{
                tabFragmentList.get(position).onResume();
            }
        }
    }

    @Override
    public void TabDataError() {

    }

    @Override
    protected void initPresenter() {
        presenter = new HomePresenter();
        presenter.attachView(this);
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.ll_home_title_search_box) {
            // 进入搜索页面
            startActivity(new Intent(getActivity(), SearchActivity.class));
        } else if (id == R.id.v_home_title_history) {
            // 进入播放历史页面
            startActivity(new Intent(getActivity(), PlayHistoryActivity.class));
        } else if (id == R.id.v_home_title_filter) {
            // 进入筛选（分类）页面
            Intent intent = new Intent(getActivity(), FindActivity.class);
            intent.putExtra(Contents.INTENT_FIND_SELECT, FindFragment.SELECT_CATEGORY);
            startActivity(intent);
        }
    }

    public void setOnClickTabListener(MovieListFragment.OnClickTabListener onClickTabListener) {
        this.onClickTabListener = onClickTabListener;
    }
}
